package renderer;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

/**
 * Cette classe permet de gerer les evenements simples sur
 * le canevas.
 * EventManager rajoute la possibilite de translater l'affichage et de zoomer.
 * 
 * @author dmitry
 */
public class EventManager implements MouseMotionListener,MouseListener, MouseWheelListener, KeyListener{

	private Renderer r;
	
	private boolean leftBtnDown;
	
	private int ox, oy;
	private long lastTranslate;
	private int accX, accY;
	
	private int cMode = 0;
	
	public EventManager(Renderer renderer)
	{
		r = renderer;
	}
	
	public void mouseClicked(MouseEvent e) 
	{
		
	}

	public void mouseEntered(MouseEvent e) 
	{
		
	}

	public void mouseExited(MouseEvent e) 
	{
		
	}

	/**
	 * Pression sur un bouton de souris
	 */
	public void mousePressed(MouseEvent e) 
	{
		if(e.getButton() == MouseEvent.BUTTON1)
		{
			leftBtnDown = true;
			ox = e.getX();
			oy = e.getY();
		}
	}

	/**
	 * Bouton de souris relache
	 */
	public void mouseReleased(MouseEvent e) 
	{
		if(e.getButton() == MouseEvent.BUTTON1)
		{
			leftBtnDown = false;
		}
	}

	/**
	 * Evenement sur la molette de souris
	 */
	public void mouseWheelMoved(MouseWheelEvent e) 
	{
		r.zoom(e.getWheelRotation());
	}

	/**
	 * Deplacement de souris avec un bouton enfonce
	 */
	public void mouseDragged(MouseEvent e) 
	{
		int dx = e.getX() - ox;
		int dy = oy - e.getY();
		if(leftBtnDown)
		{
			accX += dx;
			accY += dy;
			if(System.currentTimeMillis() - lastTranslate > 30)
			{
				r.translate(accX, accY);
				accX = 0;
				accY = 0;
				lastTranslate = System.currentTimeMillis();
			}
			ox = e.getX();
			oy = e.getY();
		}
	}

	public void mouseMoved(MouseEvent e) 
	{
		
	}

	/**
	 * Pression de touche
	 */
	public void keyPressed(KeyEvent e) {

	}

	public void keyReleased(KeyEvent e) {

	}

	public void keyTyped(KeyEvent e) {

	}

}
